package com.kingbi.system.function.service;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.log4j.Logger;

public class FunctionHelper
{
    private static FunctionHelper instance;

    FunctionService functionService;
    
    static Logger logger = Logger.getLogger(FunctionHelper.class);

    public static FunctionHelper getInstance(FunctionService funService)
    {
        if(instance == null)
        {
            instance = new FunctionHelper();
            instance.functionService = funService;
        }
        return instance;
    }

    /**
     * 构造功能树
     * 
     * @return
     * @throws SQLException 
     */
    public String createFunctionTree() throws SQLException
    {
        String sbToStr = "";
        try
        {
            String queryOrgTreeSql = "select fun.fun_id,fun.func_code fcode,fun.name fname,fun.func_level,fun.father_fun_code"
                    + " from p_function fun where fun.fun_status = 1";
            List<Object> listOrgTree = functionService.loadOrgTree(queryOrgTreeSql, null);
            BasicDynaBean bean = null;
            Iterator<Object> iterOrgTree = listOrgTree.iterator();
            StringBuffer sb = new StringBuffer();
            sb.append("[");
            while (iterOrgTree.hasNext())
            {
                bean = (BasicDynaBean) iterOrgTree.next();
                sb.append("[");
                sb.append("'" + bean.get("fcode") + "',");
                sb.append("'" + bean.get("fname") + "',");
                sb.append("'" + bean.get("father_fun_code") + "'");
                sb.append("],");
            }
            sbToStr = sb.toString();
            sbToStr = sbToStr.substring(0, sbToStr.length() - 1);
            sbToStr = sbToStr+ "]";
            logger.warn("**********************setDtree()" + sbToStr + "]");
        }      
        catch (SQLException se)
        {
            throw new SQLException("" + "FunctionHelper.createFunctionTree" + se);
        }
        return sbToStr;
    }

}
